﻿<?php
require_once('header.php');
include("adodb5/adodb-exceptions.inc.php");
include('adodb5/adodb.inc.php');

if(isset($_REQUEST['idToEdit']) && !empty($_REQUEST['idToEdit']) && is_numeric($_REQUEST['idToEdit']))
{
// se cuenta con id_principal del usuario
$idPrincipal = $_REQUEST['idToEdit'];

	$strDsn = 'mssql://influenza:inf123456@10.1.251.14';
	$strHtmlOutput = '';
	
	$cnn = ADONewConnection($strDsn);
	$cnn->SetFetchMode(ADODB_FETCH_ASSOC);
	
	if($cnn)
	{
	//
		$strSql =	"SELECT * FROM influenza.dbo.usuarios 
					WHERE id_principal = $idPrincipal";
					
		$rs = $cnn->Execute($strSql);
		
		if($rs)
		{
		//
			$intNoRows = $rs->RecordCount();
			
			if($intNoRows > 0)
			{
				while(!$rs->EOF)
				{	
				//
					$rs->FetchInto($arrCurrentRow);
					
					$strIdPrincipal = $arrCurrentRow['id_principal'];
					$strNperfil = $arrCurrentRow['nPerfil'];
					$strIdDelegacion = $arrCurrentRow['id_delegacion'];
					
					$strIdSector = $arrCurrentRow['id_sector'];
					$strIdUnidad = $arrCurrentRow['id_unidad'];
					$strNombre = $arrCurrentRow['nombre'];
					
					$strUsuario = $arrCurrentRow['usuario'];
					$strClave = $arrCurrentRow['clave'];
					$strClave2 = $arrCurrentRow['clave2'];
					
					$strActivo = $arrCurrentRow['activo'];
					
					$isMD5ok = 'v�lido';
					$isMd5okStyle = 'style=" color: #25C89D;"';
					
					if($strClave == md5($strClave2))
						{	$isMD5ok = '<div id="divClaveHash" class="controlContainer" style=" color: #FC0;">inv&aacute;lido</div>';	} 
					else 
						{	$isMD5ok = '<div id="divClaveHash" class="controlContainer" style=" color: #390;">v&aacute;lido</div>';	}
					
					if($strActivo == '1')
						{	$activeAccount = 'checked="checked"';	} 
					else 
						{	$activeAccount = '';	}
					
					//print $strIdSector;
					if ($strIdUnidad != '')
						{	/*if ($strIdSector == 1)
								{	$query = "SELECT UIST.id_entidad
									FROM Catalogos.dbo.unidades AS UIST
									INNER JOIN Catalogos.dbo.entidad AS ENT
									ON ENT.entidad = UIST.id_entidad 
									WHERE activo = 1 
									AND LEN(clues) = 11 
									and UIST.id_unidad = $strIdUnidad
									ORDER BY UIST.nombre";	
								}
							if ($strIdSector == 2) 
								{	$query = "SELECT UIMS.id_entidad
									FROM Catalogos.dbo.unidades_imss AS UIMS
									INNER JOIN Catalogos.dbo.entidad AS ENT
									ON ENT.entidad = UIMS.id_entidad 
									WHERE activo = 1 
									AND LEN(clues) = 11 
									and UIMS.id_unidad = $strIdUnidad
									ORDER BY UIMS.nombre";	
								}
							if ($strIdSector == 3) 
								{	$query = "SELECT USSA.id_entidad
									FROM Catalogos.dbo.unidades_salud AS USSA
									INNER JOIN Catalogos.dbo.entidad AS ENT
									ON ENT.entidad = USSA.id_entidad 
									WHERE activo = 1 
									AND LEN(clues) = 11 
									and USSA.id_unidad = $strIdUnidad
									ORDER BY USSA.nombre";	
								}*/
                                                        $query = "SELECT ut.id_entidad
                                                                FROM influenza..Unidades_todas ut
                                                                INNER JOIN Catalogos.dbo.entidad ENT
                                                                ON ENT.entidad = ut.id_entidad
                                                                WHERE --LEN(clues) = 11 and
                                                                ut.id_unidad = $strIdUnidad
                                                                ORDER BY ut.nombre";
								//echo "$query";
							$rs1 = $cnn->Execute($query);
							//echo "$query";
							$intNoRows1 = $rs1->RecordCount();
							while(!$rs1->EOF)
								{
									$rs1->FetchInto($arrCurrentRow1);
									$intEntidad = $arrCurrentRow1['id_entidad'];
								}
							
						}
			//
			//$strHtmlOutput = '
			?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
							<html xmlns="http://www.w3.org/1999/xhtml">
							<head>
							<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
							<title>SINAVE - Sistema Nacional de Vigilancia Epidemiol&oacute;gica</title>
							<link href="main.css" rel="stylesheet" type="text/css" />
							<link href="estilos.css" rel="stylesheet" type="text/css" />
							<script language="javascript" type="text/javascript" src="js/prototype.js"></script>
							<script language="javascript" type="text/javascript" src="js/core.js"></script>
                            <script language="javascript" type="text/javascript" src="js/validaciones.js"></script>
							<script language="javascript" type="text/javascript">
							window.onload	=	function()
							{
							//
								setTimeout(	function()
											{
											//
												//userEditInit();
												updateControl('divPerfil', 'cboPerfilesFill.php', {idPrfil: <?php echo $strNperfil; ?>});
												updateControl('divSector', 'cboSectoresFill.php', {intSector: <?php echo $strIdSector; ?>, intIndicador:1, strMetodo: 'nombre', intAgrega: '' });
												//updateControl('divDelegacion', 'cboDelegacionesFill.php', {intEntidad: <?php echo $strIdDelegacion; ?>});
                                                                                                updateControl('divDelegacion', 'cboEntidadFill.php', {intEntidad: <?php echo $intEntidad; ?>, intAgrega:''});
												updateControl('divUsmiNameList', 'cboUsmiNameFill.php', {intSector: <?php echo $strIdSector; ?>, intUnidad: <?php echo $strIdUnidad; ?>, intEntidad:<?php echo $intEntidad; ?>});
												
											}, 100 );
							}
							</script>
							</head>

							<body marginheight="0" marginwidth="0" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0">
							<div align="center">
							  <table width="780" border="0" cellpadding="0">
								<tr>
								  <td width="780" height="107"><img src="images/ssa-banner.jpg" width="780" height="107" /></td>
								</tr>
								<tr>
								  <td width="780" height="20">
							<div id="linkBar" align="left">
								  <table width="100%" >
								  <tr>
									<td width="23%">
										<strong>Bienvenido(a)</strong>&nbsp;<?php echo $usernomb; ?>
									</td>
									<td width="47%" align="left">
										<div id="divLogOut" onclick="javascript: goToUrl('../menu_administrador.php');"><strong>Inicio</strong></div>
									</td>
									<td id="logInOut" width="30%" align="right">
										<div id="divLogOut" onclick="javascript: goToUrl('m_usuarios.php');">[&nbsp;<strong>Atr&aacute;s</strong>&nbsp;]</div>
									</td>
								</tr>
							  </table>
							</div>
							</td>
								</tr>
                                <form name="form" action="update_usuario.php" method="post">
								<tr><!-- incia forma -->
								  <td>
								  <div id="divBody">
									<div id="divDefaultBody" style="width: 780px;" height="120">
<table width="780" border="0" cellpadding="0" cellspacing="0" align="left">
  <tr width="780" height="20" class="title">
    <td width="20%" class="Texto_gral">Id<br>
      <input id="txtIdPrincipal" name="txtIdPrincipal" type="text" style="background-color:#CCC; width:80px" readonly="readonly" size="5" maxlength="5" value="<?php echo $strIdPrincipal; ?>">
</td>
    <td width="30%"class="Texto_gral">&nbsp;&nbsp;&nbsp;&nbsp;Perfil
      <div id="divPerfil" class="controlContainer">
        <!--<select name="cboPerfil" id="cboPerfil" onfocus="javascript: $(\'divUserAddMsg\').update(\'Seleccione un perfil de usuario.\');" onblur="javascript: $(\'divUserAddMsg\').update(\'\');">
                <option value="0">---&nbsp;seleccione&nbsp;un&nbsp;perfil&nbsp;de&nbsp;de&nbsp;usuario&nbsp;---</option>
              </select>-->
        <select name="cboPerfil" id="cboPerfil">
          <option value="0">---&nbsp;seleccione&nbsp;un&nbsp;perfil&nbsp;de&nbsp;de&nbsp;usuario&nbsp;---</option>
          </select>
      </div>      </td>
    <td width="25%">
      <div id="divSector" class="controlContainer">
        <select name="cboSector" id="cboSector">
          <option value="0">---&nbsp;seleccione&nbsp;un&nbsp;sector&nbsp;---</option>
          </select>
        </div>    </td>
    <td width="25%"><div  class="Texto_gral"></div>
      <div id="divDelegacion" class="controlContainer">
        <select name="cboDelegacion" id="cboDelegacion">
          <option value="0">---&nbsp;seleccione&nbsp;una&nbsp;delegaci&oacute;n&nbsp;---</option>
          </select>
        </div>    </td>
  </tr>
  <tr width="780" height="20" class="info">
    <td width="25%" height="20">&nbsp;</td>
    <td width="25%" height="20">&nbsp;</td>
    <td width="25%" height="20">&nbsp;</td>
    <td width="25%" height="20">&nbsp;</td>
  </tr>
</table>

<table width="780" border="0" cellpadding="0" cellspacing="0" align="left" valign="middle">
  <tr width="780" height="25" class="info">
    <td>
    	<div id="divUsmiNameList" class="controlContainer">
              <select name="cboUsmiNameList" id="cboUsmiNameList">
                <option value="0">---&nbsp;seleccione&nbsp;una&nbsp;unidad&nbsp;&nbsp;m&eacute;dica&nbsp;---</option>
              </select>
        </div> 
    </td>
  </tr>
  <tr width="780" height="20" class="info">
    <td width="100%" height="20">&nbsp;</td>
  </tr>
</table>

<table width="780" border="0" cellpadding="0" cellspacing="0" align="left" valign="middle">
  <tr width="780" height="20" class="title">
    <td width="25%" height="20"><div class="Texto_gral">Nombre</div></td>
    <td width="25%" height="20"><div class="Texto_gral">Usuario</div></td>
    <td width="25%" height="20"><div class="Texto_gral">Contrase&ntilde;a</div></td>
    <td width="25%" height="20"><div class="Texto_gral">Hash&nbsp;Contrase&ntilde;a</div></td>
  </tr>
  <tr width="780" height="25" class="info">
    <td>
    	<div id="divNombre" class="controlContainer">
        	<input id="txtNombre" name="txtNombre" type="text" size="10" value="<?php echo htmlentities($strNombre); ?>"/>
        </div>
    </td>
    <td>
    	<div id="divUsuario" class="controlContainer">
			<input id="txtUsuario" name="txtUsuario" type="text" size="10" value="<?php echo $strUsuario; ?>"/>
        </div>
    </td>
    <td>
    	<div id="divClave" class="controlContainer">
			<input id="txtClave" name="txtClave" type="text" size="10" value="<?php echo $strClave; ?>"/>
        </div>
    </td>
    <td>
    	<div id="divClaveHash" class="controlContainer">
			<input id="txtClaveHash" name="txtClaveHash" type="text" size="10" style="background-color:#CCC;" maxlength="6" readonly="readonly" value="<?php echo $strClave2; ?>"/>
        </div> 
    </td>
  </tr>
  <tr width="780" height="20" class="info">
    <td width="25%" height="20">&nbsp;</td>
    <td width="25%" height="20">&nbsp;</td>
    <td width="25%" height="20">&nbsp;</td>
    <td width="25%" height="20"><?php echo $isMD5ok; ?></td>
  </tr>
</table>

<table width="780" border="0" cellpadding="0" cellspacing="0" align="left" valign="middle">
  <tr width="780" height="20" class="title">
    <td width="25%" height="20"><div class="Texto_gral">Habilitar&nbsp;/&nbsp;Desahabilitar</div></td>
    <td width="35%" height="20">&nbsp;</td>
    <td width="20%" height="20">&nbsp;</td>
    <td width="20%" height="20">&nbsp;</td>
  </tr>
  <tr width="780" height="25" class="info">
    <td width="25%" height="20" align="left">
    	<div id="divActivo" class="controlContainer" align="left">
			<input name="chkActivo" type="checkbox" id="chkActivo" align="left" <?php echo $activeAccount; ?> onfocus="javascript: $('divUserAddMsg').update('Activar / Desactivar usuario.');" onblur="javascript: $('divUserAddMsg').update('');"/>
        </div> 
    </td>
    <td width="35%" height="20">&nbsp;</td>
    <td width="20%" height="20">
    	<div id="divActivo" class="controlContainer" align="left">
			<input type="button" name="btnGuardar" id="btnGuardar" value="Guardar&nbsp;cambios" onclick="javascript: valida();" />
        </div> 
    </td>
    <td width="20%" height="20">
    	<div id="divActivo" class="controlContainer" align="left">
    	  <input type="button" name="btnCancelar" id="btnCancelar" value="Cancelar" onclick="javascript: goToUrl('./m_edita_usuarios.php');" />
    	</div> 
    </td>
  </tr>
</table>
<table>
  <tr width="780" height="20" class="info">
    <td width="100%" height="20"><div id="divUserAddMsg" style=" color: #390;">&nbsp;</div></td>
  </tr>
</table>
<?php //';
													}
													
														//$strHtmlOutput .= ' 
														?>
									</div>
								    </div>
										  </td>
										</tr><!-- termina form -->
                                        </form>
										<tr>
										  <td id="StatusBar" width="780" height="20" align="left"><div id="divStatusBarMsg" align="left">&nbsp;</div></td>
										</tr>
										<tr>
										  <td width="780" height="64"><img src="images/ssa-footer.jpg" width="780" height="64" /></td>
										</tr>
									  </table>
									</div>
									</body>
									</html>
                                    <?php //';
				
				//print $strHtmlOutput;
				
			} else {
			//
			}
			
			$rs->Close();
			
		} else {
		//
			print "rs fail";
		}
		
		$cnn->Close();
		
	} else {
	//
		print "fail";
	}

} else {
// el parametro proporcionado no es adecuado
	header('Location: m_add_usuarios.php');
}
?>